package cn.itcast.web.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * cookie快速入门:cookie的使用步骤
 * 当请求/cookieservletDemo1这个路径后，cookie对象绑定了一个值为hello的数据msg,
 * 然后通过response把cookie对象发送给浏览器。
 * 然后再创建一个CookieServletDemo2,当浏览器访问/cookieservletDemo2时，由于
 * 会话技术共享数据，所以是可以从request中获取到cookie的。
 * */
@WebServlet("/cookieservletDemo2")
public class CookieServletDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //3.获取cookie数据(即在这次请求中获取上次请求的数据)
        Cookie[] cookies=request.getCookies();
        //获取数据，遍历Cookies
        if(cookies!=null){
            for (Cookie c:cookies){
                String name=c.getName();
                String value=c.getValue();
                System.out.println(name+":"+value);
            }
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
